package com.tj.collection.service;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.tj.collection.db.dao.ClAllocationDetailDao;
import com.tj.collection.db.dao.ClCallDurationDao;
import com.tj.collection.db.dao.ClPhoneRecordDao;
import com.tj.collection.db.entity.ClAllocationDetailEntity;
import com.tj.collection.db.entity.ClCallDurationEntity;
import com.tj.collection.db.entity.ClPhoneRecordEntity;
import com.tj.collection.service.dto.ClPhoneRecordDto;
import com.tj.collection.utils.IndiaMobileUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/12/25
 * Time: 上午11:43
 */
@Service
public class PhoneRecordService {
    private static final Log LOGGER = LogFactory.getLog(PhoneRecordService.class);

    @Autowired
    private ClPhoneRecordDao clPhoneRecordDao;

    @Autowired
    private ClAllocationDetailDao allocationDetailDao;

    @Autowired
    private ClCallDurationDao clCallDurationDao;

    /**
     * 添加一条记录
     *
     * @param clPhoneRecordDto
     * @return
     */
    public void addRecord(ClPhoneRecordDto clPhoneRecordDto) {
        if (StringUtils.isEmpty(clPhoneRecordDto.getMobile())) {
            throw new ApplicationException("mobile is empty!");
        }
        if (StringUtils.isEmpty(clPhoneRecordDto.getCollectionNo())) {
            throw new ApplicationException("collectionNo is empty!");
        }
        if (StringUtils.isEmpty(clPhoneRecordDto.getCallingResult())) {
            throw new ApplicationException("calling result is empty!");
        }
        ClAllocationDetailEntity clAllocationDetailEntity = allocationDetailDao.getCurrentAllocationByCollectionNo(clPhoneRecordDto.getCollectionNo());
        clAllocationDetailEntity.setLastHandleTime(new Date());
        clAllocationDetailEntity.setNextTime(clPhoneRecordDto.getPromisePayDate());
        allocationDetailDao.updateByPrimaryKey(clAllocationDetailEntity);

        ClPhoneRecordEntity clPhoneRecordEntity = new ClPhoneRecordEntity();
        BeanUtils.copyProperties(clPhoneRecordDto, clPhoneRecordEntity);
        clPhoneRecordEntity.setAllocationNo(clAllocationDetailEntity.getAllocationNo());

        clPhoneRecordDao.insert(clPhoneRecordEntity);
    }

    /**
     * 增加通话记录(自动去重)
     *
     * @param collectionNo
     * @param followId
     * @param followUp
     * @param callRecords
     */
    public void addCallDuration(String collectionNo, Integer followId, String followUp, List<ClCallDurationEntity> callRecords) {
        if (CollectionUtils.isEmpty(callRecords)) {
            return;
        }
        ClAllocationDetailEntity clAllocationDetailEntity = allocationDetailDao.getCurrentAllocationByCollectionNo(collectionNo);
        for (ClCallDurationEntity clCallDurationEntity : callRecords) {
            //号码+91的处理 去除前面的+91
            clCallDurationEntity.setMobile(IndiaMobileUtil.countryCodeFilter(clCallDurationEntity.getMobile()));

            List<ClCallDurationEntity> entityList = clCallDurationDao.selectCallDurationByCollectionNo(
                    collectionNo,
                    clCallDurationEntity.getMobile(),
                    clCallDurationEntity.getStartTime(),
                    clCallDurationEntity.getEndTime());
            if (CollectionUtils.isNotEmpty(entityList)) {
                continue;
            } else {
                clCallDurationEntity.setCollectionNo(collectionNo);
                clCallDurationEntity.setAllocationNo(clAllocationDetailEntity == null ? null : clAllocationDetailEntity.getAllocationNo());
                clCallDurationEntity.setFollowId(followId);
                clCallDurationEntity.setFollowUp(followUp);
                clCallDurationDao.insert(clCallDurationEntity);
            }
        }
    }

    /**
     * 增加通话记录(自动去重)
     *
     * @param collectionNo
     * @param clCallDurationEntity
     */
    public void addCallDurationV2(String collectionNo, ClCallDurationEntity clCallDurationEntity) {
        if (clCallDurationEntity == null || StringUtils.isEmpty(collectionNo)) {
            return;
        }
        ClAllocationDetailEntity clAllocationDetailEntity = allocationDetailDao.getCurrentAllocationByCollectionNo(collectionNo);
        List<ClCallDurationEntity> entityList = clCallDurationDao.selectCallDurationByCollectionNo(
                collectionNo,
                clCallDurationEntity.getMobile(),
                clCallDurationEntity.getStartTime(),
                clCallDurationEntity.getEndTime());
        if (CollectionUtils.isNotEmpty(entityList)) {
            LOGGER.info("通话录音更新,mobile:{},startTime:{},url:{}", clCallDurationEntity.getMobile(), clCallDurationEntity.getStartTime(), clCallDurationEntity.getVideoUrl());
            if (StringUtils.isNotEmpty(clCallDurationEntity.getVideoUrl())) {
                ClCallDurationEntity entity = entityList.get(0);
                entity.setVideoUrl(clCallDurationEntity.getVideoUrl());
                clCallDurationDao.updateByPrimaryKey(entity);
            }
        } else {
            LOGGER.info("通话录音添加记录,mobile:{},startTime:{},url:{}", clCallDurationEntity.getMobile(), clCallDurationEntity.getStartTime(), clCallDurationEntity.getVideoUrl());
            clCallDurationEntity.setCollectionNo(collectionNo);
            clCallDurationEntity.setAllocationNo(clAllocationDetailEntity == null ? null : clAllocationDetailEntity.getAllocationNo());
            clCallDurationDao.insert(clCallDurationEntity);
        }

    }

}
